package com.example.animationdrawabledemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

	TextView tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
//		addContentView(new MyView(this), new LayoutParams(
//				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//		ImageView imageView = (ImageView) findViewById(R.id.testImgView);
//		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gif_2_0001);
//		Bitmap textrue = BitmapFactory.decodeResource(getResources(), R.drawable.aio_textrue_nor);
//		Bitmap bg = BitmapFactory.decodeResource(getResources(), R.drawable.aio_user_bg_nor);
//		textrue =  createRepeater(200,100, textrue,bg);
//		textrue = getTintThePicture(textrue, textrue.getWidth(),textrue.getHeight());
//		imageView.setImageBitmap(textrue);
		// TODO Auto-generated constructor stub
		findViewById(R.id.testview).setBackground(new NewBubbleDrawable(this));
	}
	
	@Override
	protected void onResume() {
		super.onResume();
//		new Handler().postDelayed(new Runnable() {
//			
//			@Override
//			public void run() {
//				Toast.makeText(MainActivity.this, ""+tv.getLineCount(), Toast.LENGTH_LONG).show();
//			}
//		}, 200);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private static Bitmap getTintThePicture(Bitmap mBitmap, int picw, int pich) {
		int[] pix = new int[picw * pich];
		mBitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);
		int R = 0, G = 0, B = 0;
		for (int y = 0; y < pich; y++)
			for (int x = 0; x < picw; x++) {
				int index = y * picw + x;
				int r = (pix[index] >> 16) & 0xff;
				int g = (pix[index] >> 8) & 0xff;
				int b = pix[index] & 0xff;
				R = 255 - r;
				G = 255 - g;
				B = 255 - b;
				R = (R < 0) ? 0 : ((R > 255) ? 255 : R);
				G = (G < 0) ? 0 : ((G > 255) ? 255 : G);
				B = (B < 0) ? 0 : ((B > 255) ? 255 : B);
//				pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
				if(R > 200 && G > 200 && B > 200){
					pix[index] = 0x00000000;
				}
			}
//		for(int i = 0;i < pix.length;i++){
//			if(pix[i] == 0xffffffff){
//				pix[i] = 0xffff00ff;
//			}
//		}
		Bitmap bm = Bitmap.createBitmap(picw, pich, Bitmap.Config.ARGB_8888);
		bm.setPixels(pix, 0, picw, 0, 0, picw, pich);
		if(bm!=null && mBitmap!=null && !mBitmap.isRecycled()){
			mBitmap.recycle();
		}
		return bm ;
	}
	
	public static Bitmap createRepeater(int width,int height,Bitmap src,Bitmap bg) {
		int count = (width + src.getWidth() - 1) / src.getWidth();
		int heightcount = (height + src.getHeight() - 1) / src.getHeight();
		Bitmap bitmap = Bitmap.createBitmap(width,height,Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		for (int idx = 0; idx < count; ++idx) {
			for(int idx2 = 0; idx2 < heightcount; ++idx2){
				canvas.drawBitmap(src, idx * src.getWidth(), idx2 * src.getHeight(), null);
			}
		}
		Rect srRect = new Rect(0, 0, bg.getWidth(), bg.getHeight()); 
		Rect destRect = new Rect(0, 0, width, height); 
		canvas.drawBitmap(bg,srRect, destRect, null);
		return bitmap;
	}
}
